package com.dc.chat.websocket.eventhandler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dc.chat.app.service.mp.FriendService;
import com.dc.chat.app.service.mp.UserService;
import com.dc.chat.entity.dao.Friend;
import com.dc.chat.entity.dao.User;
import com.dc.chat.websocket.event.LoginEvent;
import com.dc.chat.websocket.event.LogoutEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class LogoutHandler implements ApplicationListener<LogoutEvent> {
    @Resource
    FriendService friendService;
    @Resource
    UserService userService;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    SimpMessagingTemplate simpMessagingTemplate;
    @Override
    public void onApplicationEvent(LogoutEvent event) {
        String username = event.getUsername();
        User user = userService.selectByUserName(username);
        Collection<String> usernames=friendService.getFriendsUsername(username);
        Map<String,String> loginUser = redisTemplate.opsForHash().entries("login");
        for (String s : usernames) {
            String s1 = loginUser.get(s);
            if(s1!=null){
                //有备注显示备注 没有就显示用户名
                User friendUser = userService.selectByUserName(s);
                Friend one = friendService.getOne(Wrappers.lambdaQuery(new Friend())
                        .eq(Friend::getUserId, friendUser.getId())
                        .eq(Friend::getFriendId, user.getId())
                );

                String back = one.getBack();
                Map map=new HashMap();
                map.put("logout",true);
                map.put("username",username);
                if(back!=null){
                    simpMessagingTemplate.convertAndSend("/u/"+s1,back+"用户下线了",map);
                }else{
                    simpMessagingTemplate.convertAndSend("/u/"+s1,username+"用户下线了",map);
                }
            }
        }
        //给管理员发送在线人数
        Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
        Map login = redisTemplate.opsForHash().entries("login");
        int size = login.size();
        Map map1=new HashMap();
        map1.put("onlineNumChange",true);
        for (String adminToken : adminTokens) {
            simpMessagingTemplate.convertAndSend("/back/"+adminToken,size,map1);
        }
        //告诉所有管理员 重新获取列表
        Map map=new HashMap();
        map.put("refresh",true);
        for (String adminToken : adminTokens) {
            simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
        }
    }
}
